package net.zhengxinyun.performance.controller.ExamNew;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alipay.api.request.AlipayDataDataserviceSdfsdfRequest;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.dto.examDTO.ExamDTO;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.exam.*;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.filter.exam.*;
import net.zhengxinyun.performance.service.examNew.InquireObjService;
import net.zhengxinyun.performance.service.examNew.PaperService;
import net.zhengxinyun.performance.service.examNew.QuestionnaireService;
import net.zhengxinyun.performance.service.examNew.UnSysUserService;
import net.zhengxinyun.performance.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.security.sasl.SaslServer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Date: 2020/3/12 14:38
 * 问卷对象管理
 */


@RestController
@RequestMapping("/exam/User")
public class ExamUserCtrl {

    @Autowired
    private UnSysUserService examUsersService;
    @Autowired
    private InquireObjService inquireObjService;
    @Autowired
    private UserService userService;

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
   private  PaperService paperService;

    /**
     * 测评对象的管理
     * @param param
     * @return
     */
    @PostMapping("/userManage")
    public Result<HashMap<String, Object>> userManage(@RequestBody ExamUserParam param){

        if(ObjectUtils.isBlank(param.filter))
        {
            return ResultUtils.getResults(Result.Status.ERROR, "keyone不能为空", null);

        }
        String optionType = param.filter.getOptionType();
        if(StringUtils.isBlank(optionType))
        {
            return ResultUtils.getResults(Result.Status.ERROR, "optionType", null);

        }
        UnSysUsers record = new UnSysUsers();
        record.setUserName( param.filter.getUserName());
        record.setUserSex ( param.filter.getUserSex());
        record.setUserType( param.filter.getUserType());
        record.setUserTel ( param.filter.getUserTel());

        switch (optionType){

            case "query":
                List<UnSysUserFilter> unSysUsers1 = examUsersService.seletListUsers( param.filter);
                HashMap<String,Object> map = new HashMap<>();
                map.put("data",unSysUsers1);
                return ResultUtils.getSuccessResults(map);
            case "add":

                record.setId(null);
                int insert = examUsersService.insert(record);
                break;
            case "update":
                record.setId( param.filter.getId());
                if(ObjectUtils.isBlank(record.getId())){
                    return ResultUtils.getResults(Result.Status.ERROR, "用户id不能为空", null);
                }
                examUsersService.updateByPrimaryKey(record);
                break;
            case "del":
                record.setId( param.filter.getId());
                record.setId( param.filter.getId());
                if(ObjectUtils.isBlank(record.getId())){
                    return ResultUtils.getResults(Result.Status.ERROR, "用户id不能为空", null);
                }
                examUsersService.deleteByPrimaryKey(record.getId());
                break;
            default:
                return ResultUtils.getResults(Result.Status.ERROR, "optionType参数错误", null);

        }


        return ResultUtils.getSuccessResults(null);
    }
    /**
     * 用户类型管理
     * @return
     */
    @PostMapping("/userTypeManage")
    public Result<HashMap<String, Object>> userTypeManage(@RequestBody ExamUserParam param)
    {
        if(ObjectUtils.isBlank(param.typeFilter))
        {
            return ResultUtils.getResults(Result.Status.ERROR, "keyone不能为空", null);

        }
        String optionType = param.typeFilter.getOptionType();
        UserTypeEntity record = new UserTypeEntity();
        record.setUserType(param.typeFilter.getUserType());
        switch (optionType){
            case "query":
                List<UserTypeEntity> typeLists = examUsersService.seletListTypes(param.typeFilter);
                HashMap<String ,Object> map = new HashMap<>();
                map.put("data",typeLists);
                return ResultUtils.getSuccessResults(map);
            case "del":
                if(ObjectUtils.isBlank(param.typeFilter.getId()))
                {
                    return ResultUtils.getResults(Result.Status.ERROR, "用户id不能为空", null);
                }
                int i = examUsersService.deleteTypeByPrimaryKey(param.typeFilter.getId());
                break;
            case "update":
                record.setId(param.typeFilter.getId());
                examUsersService.updateTypeByPrimaryKeySelective(record);
                break;
            case "add":
                record.setId(null);
                examUsersService.insertType(record);
                break;
        }


        return ResultUtils.getSuccessResults(null);
    }


    /**
     * 调查对象总表管理 web
     */
    @PostMapping("/inquireObj")
    public Result<HashMap<String, Object>> inquireObj(@RequestBody ExamUserParam filter) {
        String optionType = filter.inqObjFilter.getOptionType();
        InquireObjEntity record = new InquireObjEntity();

        record.setInquireName(filter.inqObjFilter.getInquireName());
        record.setInquireSource(filter.inqObjFilter.getInquireSource());

        switch (optionType) {
            case "query":
                List<InquireObjEntity> inquireObjEntities = inquireObjService.selectListObjs(filter.inqObjFilter);
                HashMap<String, Object> map = new HashMap<>();



                map.put("data", inquireObjEntities);
                System.out.println("------/inquireObj");
                return ResultUtils.getSuccessResults(map);
            case "del":
                if (ObjectUtils.isBlank(filter.inqObjFilter.getInqId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "id不能为空", null);
                }
                inquireObjService.deleteObjByPrimaryKey(filter.inqObjFilter.getInqId());
                break;
            case "update":
                record.setId(filter.inqObjFilter.getInqId());
                inquireObjService.updateObjByPrimaryKeySelective(record);
                break;
            case "add":

                record.setId(null);
                inquireObjService.insertObj(record);
                break;
            default:
                return ResultUtils.getResults(Result.Status.ERROR, "optionType类型错误", null);

        }

        System.out.println("-----/inquireObj");
        return ResultUtils.getSuccessResults(null);
    }


   private  List<UnSysUserFilter> getInqListObjs(String userSource){
       List<UnSysUserFilter> unSysUsers = new ArrayList<>();
        switch (userSource){
            case "系统":
                List<UserFilter> userFilters = userService.queryUsers(new UserFilter());
                for (UserFilter item : userFilters) {
                    UnSysUserFilter user = new UnSysUserFilter();
                    user.setId(item.getId());
                    user.setUserName(item.getUserName());
                    user.setUserTel(item.getUserPhone());
                    user.setUserType("");
                    unSysUsers.add(user);
                }
                break;
            case "非系统":
                UnSysUsers filter = new UnSysUsers();
                //有过滤条件 选用过滤条件

                //无过滤条件 直接选择全部
                    unSysUsers = examUsersService.seletListUsers(new UnSysUserFilter());


                break;
            default:
                return  unSysUsers;

        }

        return  unSysUsers;
    }


    /**
     * 调查对象信息 （抽取调查人员）
     */
    @PostMapping("/inquireInfo")
    public Result<HashMap<String, Object>> inquireInfo(@RequestBody ExamUserParam filter) {
        String optionType = filter.inqObjFilter.getOptionType();
        InquireInfoEntity record = new InquireInfoEntity();
        record.setInqId(filter.inqObjFilter.getInqId());
        record.setUserId(filter.inqObjFilter.getUserId());
        if (StringUtils.isBlank(optionType)) {
            return ResultUtils.getResults("500","optionType",null);
        }
        switch (optionType) {
            case "query":
                HashMap<String, Object> map = new HashMap<>();
                List<InquireObjFilter> inquireInfoEntities = inquireObjService.selectListInfos(filter.inqObjFilter);
                if(ObjectUtils.isBlank(inquireInfoEntities))
                {
                    //空的 去判断调查来源
                    String inquireSource = filter.inqObjFilter.getInquireSource();
                    if (StringUtils.isBlank(inquireSource)) {
                        return ResultUtils.getResults("500","调查来源不能为i空",null);
                    }
                    List<UnSysUserFilter> inqListObjs = getInqListObjs(inquireSource);
                    map.put("data", inqListObjs);
                    map.put("isExist",0);
                    System.out.println("------/inquireInfo");
                    return ResultUtils.getSuccessResults(map);
                }

                map.put("data", inquireInfoEntities);
                map.put("isExist",1);
                System.out.println("------/inquireInfo");
                return ResultUtils.getSuccessResults(map);
            case "del":

               // List<String> strings = Util.strToList(filter.inqObjFilter.getInqInfoIds());
                List<Long> userIdList = filter.inqObjFilter.getUserIdList();
                if (ObjectUtils.isBlank(userIdList)) {
                    return ResultUtils.getResults(Result.Status.ERROR, "勾选的用户ID列表不能为空", null);
                }
                for (Long inqId : userIdList) {
                    inquireObjService.deleteInfoByPrimaryKey(inqId);

                }

                break;
            case "update":
                record.setId(filter.inqObjFilter.getInqInfoId());
                inquireObjService.updateInfoByPrimaryKeySelective(record);
                break;
            case "add":
                List<Long> userIdLists = filter.inqObjFilter.getUserIdList();
                if (ObjectUtils.isBlank(userIdLists)||ObjectUtils.isBlank(filter.inqObjFilter.getInqId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "调查总表的Id和用户勾选列表userIdlists不能为空", null);
                }

               // List<String> ids = Util.strToList(userIdLists);
                List<InquireInfoEntity> listRecord = new ArrayList<>();

                for (Long id : userIdLists) {

                    InquireInfoEntity recorditem = new InquireInfoEntity();
                    recorditem.setUserId(id.intValue());
                    recorditem.setInqId(filter.inqObjFilter.getInqId());
                    recorditem.setId(null);
                    listRecord.add(recorditem);
                }

                int i = inquireObjService.insertInfo(listRecord);
                break;
            default:
                return ResultUtils.getResults(Result.Status.ERROR, "optionType类型错误", null);

        }

        System.out.println("-------/inquireInfo");
        return ResultUtils.getSuccessResults(null);
    }




    /**
     * 问卷总表的管理
     * @return
     */
    @PostMapping("/questionnaire")
    public Result<HashMap<String, Object>> qustionnaireManage(@RequestBody ExamUserParam param){

        String optionType = param.quesFilter.getOptionType();
        if (StringUtils.isBlank(optionType)) {
            ResultUtils.getResults("500","optionType不能唯恐");
        }
        HashMap<String, Object> maps = questionnaireService.questionnaireManage(optionType, param.quesFilter);
        if (ObjectUtils.isBlank(maps)) {
            ResultUtils.getResults("500","参数错误",maps);
        }

        return  ResultUtils.getSuccessResults(maps);
    }
    /**
     * 问卷倒入校验（一般，单位，工作）
     * @return
     */
    @PostMapping("/checkQust")
    public Result<HashMap<String, Object>> createQust(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "questionnaireId") Long questionnaireId){



        List<ExamDTO> list = questionnaireService.createQuestionnaire( questionnaireId, file);
        if (list == null) {
            return ResultUtils.getResults("500", "参数错误", null);
        }
        HashMap<String,Object> ham = new HashMap<>();
        ham.put("data",list);

        return  ResultUtils.getSuccessResults(ham);
    }
    @PostMapping("/saveQust")
    public Result<String> saveQust(@RequestBody ExamUserParam param){

        String examType = param.quesFilter.getExamType();
        List<ExamDTO> examlists = param.quesFilter.getExamlists();
        if(StringUtils.isBlank(examType)||ObjectUtils.isBlank(examlists)){
            return  ResultUtils.getResults("500","examtype,examlists,不能为空");
        }
        int i = questionnaireService.saveExams(examType, examlists);
        if(i==-1){
            return  ResultUtils.getResults("500","examtype参数错误",null);

        }

        return  ResultUtils.getSuccessResults();
    }

    /**
     * 获取问卷
     * @param param
     * @return
     */
    @PostMapping("/getExamPaper")
    public Result<HashMap<String, Object>> priviewPages(@RequestBody ExamUserParam param){

        String examType = param.quesFilter.getExamType();
        Long questionnaireId = param.quesFilter.getQuestionnaireId();
        if (StringUtils.isBlank(examType)||ObjectUtils.isBlank(questionnaireId)) {
            return ResultUtils.getResults("500","examType和questionnaireId不能空",null);
        }
        List<ExamDTO> examDTOS = paperService.priviewPages(examType, questionnaireId);

        HashMap<String,Object> map =new HashMap<>();
        map.put("data",examDTOS);
        return ResultUtils.getSuccessResults(map);


    }

}

class ExamUserParam
{
    public UnSysUserFilter filter;
    public UnSysUsers      entity;
    public UsersTypeFilter  typeFilter;
    public TokenEntity     tokenEntity;
    public PingyiFilter pingyiFilter;
    public InquireObjFilter inqObjFilter;
    public QuestionnaireFilter quesFilter;

}